package com.masterclient.app.businessmodule.homemodule.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.adapter.MasterMienAdapter;
import com.masterclient.app.businessmodule.homemodule.contract.MasterMienContract;
import com.masterclient.app.businessmodule.homemodule.contract.MasterMienPresenter;
import com.masterclient.app.modules.coupon.beans.NewCouponBean;
import com.masterclient.app.modules.income.model.MasterMienDataBean;
import com.masterclient.app.modules.income.model.MeetReportDataBean;
import com.masterclient.app.modules.income.model.MineDataBean;
import com.masterclient.app.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/4 13:17
 * 功能说明：
 */
@Route(path = AroutePath.MASTER_MIEN_FRAGMENT)
public class MasterMienFragment extends BaseFragment<MasterMienPresenter> implements MasterMienContract.View {
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private MasterMienDataBean dataBean;
    List<MasterMienDataBean.MastersItem> list;
    private boolean hasMore = true;
    private int page = 1;

    private MasterMienAdapter recycleAdapter;

    public static MasterMienFragment getInstance() {
        return new MasterMienFragment();
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_master_mien, null);
    }

    @Override
    protected void initOnlyOnce() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleAdapter = new MasterMienAdapter();
        recycleAdapter.setOnItemClickListener((adapter, view, position) -> {
            MasterMienDataBean.MastersItem item = list.get(position);
            CommonUtil.goAnyWhere(mContext, item.url, "", "", "", "", "");
        });
        recyclerView.setAdapter(recycleAdapter);
        recycleAdapter.bindToRecyclerView(recyclerView);

        page = 1;
        mPresenter.getMasters(String.valueOf(page), PAGE_SIZE);
        recycleAdapter.setOnLoadMoreListener(() -> {
            if (hasMore) {
                page++;
                mPresenter.getMasters(String.valueOf(page), PAGE_SIZE);
            }
        }, recyclerView);

        swipeLayout.setOnRefreshListener(() -> {
            if (list != null) {
                list.clear();
                recycleAdapter.notifyDataSetChanged();
            }
            recycleAdapter.setEnableLoadMore(true);
            page = 1;
            mPresenter.getMasters(String.valueOf(page), PAGE_SIZE);

        });
        super.initOnlyOnce();
    }

    @OnClick({R.id.rl_back, R.id.iv_img, R.id.tv_more})
    protected void onClick(View view) {
        if (view.getId() == R.id.rl_back) {
            getActivity().finish();
        } else if (view.getId() == R.id.iv_img) {
            if (dataBean.adsMaster != null && !TextUtils.isEmpty(dataBean.adsMaster.targetUrl)) {
                CommonUtil.goAnyWhere(mContext, dataBean.adsMaster.targetUrl, "", "", "", "", "");
            }
        } else if (view.getId() == R.id.tv_more) {
            tvDes.post(() -> {
                //获取省略的字符数，0表示没和省略
                int ellipsisCount = tvDes.getLayout().getEllipsisCount(tvDes.getLineCount() - 1);
                tvDes.getLayout().getEllipsisCount(tvDes.getLineCount() - 1);
                //ellipsisCount>0说明没有显示全部，存在省略部分。
                if (ellipsisCount > 0) {
                    //展示全部，按钮设置为点击收起。
                    tvDes.setMaxHeight(getResources().getDisplayMetrics().heightPixels);
                    tvMore.setVisibility(View.GONE);
                } else {
                    //显示2行，按钮设置为点击显示全部。
                    tvDes.setMaxLines(2);
                    tvMore.setVisibility(View.VISIBLE);
                }
            });
        }
    }

    @Override
    public void getMastersInfoSuccess(MasterMienDataBean dataBean) {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }

        this.page = Integer.parseInt(dataBean.page);
        if (list == null) {
            list = new ArrayList<>();
        }
        if (page == 1) {
            //头部信息
            this.dataBean = dataBean;
            if (!TextUtils.isEmpty(dataBean.description)) {
                tvDes.setText(dataBean.description);
                tvDes.post(() -> {
                    //获取省略的字符数，0表示没和省略
                    int ellipsisCount = tvDes.getLayout().getEllipsisCount(tvDes.getLineCount() - 1);
                    tvDes.getLayout().getEllipsisCount(tvDes.getLineCount() - 1);
                    //ellipsisCount>0说明没有显示全部，存在省略部分。
                    if (ellipsisCount > 0) {
                        //展示全部，按钮设置为点击收起。
                        tvMore.setVisibility(View.VISIBLE);
                    } else {
                        //显示2行，按钮设置为点击显示全部。
                        tvMore.setVisibility(View.GONE);
                    }
                });
            }
            if (dataBean.adsMaster != null && !TextUtils.isEmpty(dataBean.adsMaster.banner)) {
                GlideUtil.displayImage(mContext, dataBean.adsMaster.banner, ivImg, R.mipmap.defaults);
            }

            list.clear();
            page++;
            mPresenter.getMasters(String.valueOf(page), PAGE_SIZE);
        }
        list.addAll(dataBean.masters);
        recycleAdapter.setNewData(list);
        recycleAdapter.setEnableLoadMore(true);
        if (page > 1 && (dataBean.masters == null || dataBean.masters.size() == 0)) {
            hasMore = false;
            recycleAdapter.setEnableLoadMore(false);
        }
        recycleAdapter.notifyDataSetChanged();
    }

    @Override
    public void getMastersInfoFailed(String msg) {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }
}
